package com.bdqn.Practice.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.Practice.pojo.SysUser;
import com.bdqn.Practice.service.SysRoleService;
import com.bdqn.Practice.service.SysUserService;
import com.bdqn.Practice.util.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/user")
@MultipartConfig
public class SysUserMapperController {
    @Resource
    private SysUserService service; //引入service层
    @Resource
    private SysRoleService roleService;
    @GetMapping(value = "tologin")
    public String tologin(){
        return "login";
    }
    @PostMapping(value = "login") //给方法设置进入路径
    public String login(String account, String password, Model model, HttpSession session){
        SysUser userService=service.userList(account,password); //把查询结果放进SysUser实体类里
        if (userService==null){
            //登录失败账号或密码错误从新进入登录页面
            model.addAttribute("error","账号或密码错误");
            return "login";
        }
        //登录成功把查询结果放进session作用域里在放回要进入的jsp页面
        session.setAttribute("USER_SESSION",userService);
        return "frame";
    }
    @GetMapping(value = "logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/user/tologin";
    }

//    @ExceptionHandler(value = {RuntimeException.class})
//    public String handlerException(RuntimeException e, HttpServletRequest request){
//        request.setAttribute("e",e);
//        return "error";
//    }

    @GetMapping(value = "list")
    public String userList(Model model,@RequestParam(defaultValue = "1") Integer pageIndex,String queryRealName,Integer queryRoleId ){
        Page page=new Page();
        page.setTotalCount(service.count(queryRealName,queryRoleId));
        page.setCurrentPageNo(pageIndex);
        page.setList(service.List(queryRealName,queryRoleId,page.getCurrentPageNo(),page.getPageSize()));
        model.addAttribute("roleList",roleService.roleName());

        model.addAttribute("userList",page.getList());
        model.addAttribute("page",page);
        return "sysUser/list";
    }

    @GetMapping(value = "add")
    public String toAdd(Model model){
        model.addAttribute("roleList",roleService.roleName());
        return "sysUser/add";
    }

    @PostMapping(value = "/add")
    public String insert(SysUser sysUser){
        System.out.println(sysUser.getRealName());
        int row=service.addUser(sysUser);
        if (row>0){
            return "sysUser/list";
        }
        return "sysUser/add";
    }

    @ResponseBody
    @GetMapping(value = "userExist")
    public String userExist(String account, HttpServletResponse response) throws IOException {
        int exist=service.getByCount(account);
        Map<String,Integer> map=new HashMap<>();
        map.put("exist",exist);
        String resultJson= JSON.toJSONString(map);
        return resultJson;
    }

    @GetMapping(value = "/toUpdate")
    public String toUpdate(Model model,Integer uid){
        model.addAttribute("sysUser",service.selectAll(uid));
        return "sysUser/update";
    }

    @PostMapping(value = "/update")
    public String doUpdate(Model model,SysUser sysUser){
        if (service.updateAll(sysUser)>0){
            Page page=new Page();
            page.setTotalCount(service.count(null,null));
            page.setCurrentPageNo(1);
            page.setList(service.List(null,null,page.getCurrentPageNo(),page.getPageSize()));
            model.addAttribute("roleList",roleService.roleName());
            model.addAttribute("userList",page.getList());
            model.addAttribute("page",page);
            return "sysUser/list";
        }
        return "sysUser/update";
    }


}
